package com.laf.mall.api.repository;

import com.laf.mall.api.dto.MemberSetting;
import com.laf.mall.api.dto.PointsDeduction;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * @author ygl
 * @create 2019-01-02
 * @DESCRIPTION
 **/
@Repository
@Slf4j
public class PointsDeductionRepository {

    @Autowired
    JdbcTemplate jdbcTemplate;


    /**
     * 新增数据
     * @param model
     * @return
     */
    public int insert(PointsDeduction model){
        String sql = "INSERT INTO t_points_deduction(offset_number,member_id,car_number,total_cost,deduction_cost," +
                "deduction_points,status,frozen_date,biz_date) VALUES (?,?,?,?,?,?,?,?,?)";
        return jdbcTemplate.update(sql,new Object[]{model.getOffsetNumber(),model.getMemberId(),model.getCarNumber(),model.getTotalCost()
        ,model.getDeductionCost(),model.getDeductionPoints(),model.getStatus(),model.getFrozenDate(),model.getBizDate()});
    }

    public List<PointsDeduction> findByOffsetNumber(String offsetNumber) {
        String sql = "SELECT * FROM t_points_deduction WHERE offset_number = ?";
        List<PointsDeduction> list = jdbcTemplate.query(sql,new Object[]{offsetNumber},new PointsDeduction());
        return list;
    }

    public int updateStatus(int id, int status, Date bizDate) {
        String sql = "update t_points_deduction set status=?,biz_date=? where id=?";
        return jdbcTemplate.update(sql, new Object[]{status,bizDate,id});
    }

    public List<PointsDeduction> findByMemberId(Integer memberId) {
        String sql = "SELECT * FROM t_points_deduction WHERE status = 2 and member_id = ?";
        List<PointsDeduction> list = jdbcTemplate.query(sql,new Object[]{memberId},new PointsDeduction());
        return list;
    }
}
